unit UntFrmGrafico;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls,
  FIBDatabase, pFIBDatabase, Buttons, CandleStick, StdCtrls,
  Spin, FIBQuery, pFIBQuery;

type
  TFrmGraf = class(TForm)
    TrGraf: TpFIBTransaction;
    pnl1: TPanel;
    btn1: TSpeedButton;
    btn2: TSpeedButton;
    btn3: TSpeedButton;
    btn4: TSpeedButton;
    btn5: TSpeedButton;
    Timer1: TTimer;
    CbTipoGraf: TComboBox;
    Label1: TLabel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    Label2: TLabel;
    Label3: TLabel;
    EdtMMA: TSpinEdit;
    EdtMME: TSpinEdit;
    SpeedButton3: TSpeedButton;
    QryGraf: TpFIBQuery;
    CandleStick1: TCandleStick;
    procedure btn4Click(Sender: TObject);
    procedure mClose(Sender: TObject; var Action: TCloseAction);
  private
    tipoGraf: string;
  public
    acao: string;
  end;



implementation

uses UntDm;

{$R *.dfm}

procedure TFrmGraf.btn4Click(Sender: TObject);
begin
  if (acao = '') or (not DM.DB.Connected) then Exit;

  try
    Timer1.Enabled := False;
    if sender is TSpeedButton then tipoGraf := TSpeedButton(Sender).Caption;

    with CandleStick1 do begin
      clear;

       with QryGraf DO begin
        Close;
        if not Transaction.InTransaction then Transaction.StartTransaction;
        
        SQL.Clear;
        SQL.Add('SELECT * FROM');
        SQL.Add('(SELECT FIRST 1800 * FROM COTACOES WHERE ACAO = :ACAO ORDER BY DATA DESC)');
        SQL.Add('ORDER BY DATA ASC');
        ParamByName('ACAO').AsString := acao;
        ExecQuery;
        while not eof do begin
          add(FieldByName('ABERTURA').AsCurrency,
              FieldByName('MAXIMO').AsCurrency,
              FieldByName('MINIMO').AsCurrency,
              FieldByName('ENCERRAMENTO').AsCurrency,
              FieldByName('DATA').AsDate);
          Next;
        end;
        Close;
      end;

      case CbTipoGraf.ItemIndex of
        0: TipoGrafico := grafCandle;
        1: TipoGrafico := grafBarra;
        2: TipoGrafico := grafLinha;
      end;

      if (tipoGraf = '1D') then begin
        Periodo := per1D;
      end else if (tipoGraf = '5D') then begin
        Periodo := per5D;
      end else if (tipoGraf = '1M') then begin
        Periodo := per1M;
      end else if (tipoGraf = '2M') then begin
        Periodo := per2M;
      end else if (tipoGraf = '3M') then begin
        Periodo := per3M;
      end else if (tipoGraf = '6M') then begin
        Periodo := per6M;
      end else if (tipoGraf = '1A') then begin
        Periodo := per1A;
      end else if (tipoGraf = '2A') then begin
        Periodo := per2A;
      end;
      MMA := StrToIntDef(EdtMMA.Text,0);
      MME := StrToIntDef(EdtMME.Text,0);
      plotarGrafico;
    end;
  finally
    Timer1.Enabled := True;
  end;
end;

procedure TFrmGraf.mClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

end.
